package cn.edu.zufe.mjt;

import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author 蒙锦涛
 * @date 2025/6/6
 */
public class Compiler {
	public static void compile(File javaFile) {
		JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

		try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null)) {
			Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(List.of(javaFile));

			List<String> options = List.of("-d", "./target/classes");

			JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, options, null, compilationUnits);

			Boolean success = task.call();

			if (success) {
				System.out.println("编译成功");
			} else {
				System.out.println("编译失败");
			}

		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}














}
